import {
    Behaviour,
    core,
    getGameObjectById,
    HitTestScript,
    SpriteRenderer,
    TextRenderer,
    Transform
} from "../gameengine";
import {
    keyChineseNameDictionary,
    keyDescriptionsDictionary,
    keyPreviewDictionary,
    keyPropertyDictionary
} from "./backPack";
import {framPrefab, previewPrefab, useBtnPrefab} from "../data/data";
import {user} from "../user";
import {gameEventBus} from "../GameEventBus";
import { music } from "../music/music";

export class IconBehaviour extends Behaviour {
    private hitTest: HitTestScript;
    public objname: string;
    private transform: Transform;

    protected onStart() {
        this.hitTest = this.displayObject.getScript(HitTestScript);
        this.transform = this.displayObject.getScript(Transform);

        this.hitTest.onClick = () => {
            this.selectObjInBackpack();
            this.updateDescription();

            if (keyPropertyDictionary[this.objname] == "Special") {
                this.objPreView();
            }
        };
    }

    protected onUpdate(advancedTime: number) {
    }

    private updateDescription() {
        const des = getGameObjectById("description");
        const desTxt = des.getScript(TextRenderer);
        desTxt.text = keyDescriptionsDictionary[this.objname];

        const desName = getGameObjectById("descriptionName");
        const desNameTxt = desName.getScript(TextRenderer);
        desNameTxt.text = keyChineseNameDictionary[this.objname];

        const backPackPrefab = getGameObjectById("backPackPrefab");
        if (getGameObjectById("framePrefab")) {
            backPackPrefab.removeChild(getGameObjectById("framePrefab"));
        }

        //LeeYushan
        if(getGameObjectById("useBtnPrefab")){
            console.log("remove useBtn");
            const desBg=getGameObjectById("desBg");
            desBg.removeChild(getGameObjectById("useBtnPrefab"));
        }

        //如果物品可以使用
        if(this.objname=="fire"||this.objname=="gun"){

            const useBtn=core.createPrefab(useBtnPrefab);
            const desBg=getGameObjectById("desBg");
            desBg.addChild(useBtn);

            const transform=useBtn.getScript(Transform);
            transform.x=410;
            transform.y=50;

            const hitTest=useBtn.getScript(HitTestScript);
            hitTest.onClick=()=>{

                for(var i=0;i<user.useBagItems.length;i++){
                    if(this.objname==user.useBagItems[i]){
                        return;
                    }
                }

                user.useBagItems.push(this.objname);
                console.log(user.useBagItems);

                const icon=getGameObjectById(this.objname+"icon");
                console.log("getIcon");
                console.log(this.objname+"icon");
                console.log(icon);

                const spriteRender=icon.getScript(SpriteRenderer);
                spriteRender.imageName="assets/picture/backpack/"+this.objname+"_grey.png";

                gameEventBus.dispatch("use",this.objname);
                if(this.objname == "gun"){
                    music.shootPlay();
                }
            }
        }

        const framePrefab = core.createPrefab(framPrefab);
        backPackPrefab.addChild(framePrefab);
        const frameTransform = framePrefab.getScript(Transform);
        frameTransform.x = this.transform.x - 2;
        frameTransform.y = this.transform.y - 2;
    }

    private objPreView() {
        const container = getGameObjectById("Container");
        const prefab = core.createPrefab(previewPrefab);
        container.addChild(prefab);

        const spriteRender = prefab.getScript(SpriteRenderer);

        spriteRender.imageName = keyPreviewDictionary[this.objname][0];

        const hitTest = prefab.getScript(HitTestScript);
        hitTest.onClick = () => {
            const array = keyPreviewDictionary[this.objname];
            const index = array.indexOf(spriteRender.imageName);

            if (index < array.length) {
                spriteRender.imageName = array[index + 1];
            } else {
                container.removeChild(prefab);
            }

        }
    }

    private selectObjInBackpack() {
        const backPackPrefab = getGameObjectById("backPackPrefab");

        if (getGameObjectById("framePrefab")) {
            backPackPrefab.removeChild(getGameObjectById("framePrefab"));
        }


        const framePrefab = core.createPrefab(framPrefab);
        backPackPrefab.addChild(framePrefab);
        const frameTransform = framePrefab.getScript(Transform);
        frameTransform.x = this.transform.x - 2;
        frameTransform.y = this.transform.y - 2;
    }
}